package com.nidu.demo.company.executor.query;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.company.convertor.CompanyConvertor;
import com.nidu.demo.company.convertor.CompanyCriteriaConvertor;
import com.nidu.demo.company.dto.CompanyCO;
import com.nidu.demo.company.dto.CompanyListQry;
import com.nidu.demo.company.gateway.CompanyGateway;
import com.nidu.demo.company.model.Company;
import com.nidu.demo.company.model.CompanyCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 公司列表查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class CompanyListQryExe {

    private final CompanyGateway gateway;

    private final CompanyConvertor convertor;

    private final CompanyCriteriaConvertor criteriaConvertor;

    public MultiResponse<CompanyCO> execute(CompanyListQry qry) {
        CompanyCriteria criteria = criteriaConvertor.toDataObject(qry);
        List<Company> companyList = gateway.listByCondition(criteria);
        if(CollUtil.isEmpty(companyList)){
            throw new BizException(ErrorCodeConstants.COMPANY_NOT_FOUND.getCode(), ErrorCodeConstants.COMPANY_NOT_FOUND.getMessage());
        }
        List<CompanyCO> companyCOS = convertor.toClientObjects(companyList);
        return MultiResponse.of(companyCOS);
    }

}